package ui.finance;

import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.List;

import javax.swing.JPanel;

import tool.MyButton;
import vo.AccountorVo;
import vo.UserVo;
import vo.Vo;
import blservice.finance.AccountorBLService;

import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import client.boot.BLServiceManager;
import o.Authority;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class AccountorManagementPanel extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private AccountorBLService accountorBL;
	
	private UserVo user;
	private JTextField textField;
	private JTable table;
	private JScrollPane jsp;
	private List<AccountorVo> accounts;
	public AccountorManagementPanel(UserVo user){
		if(user.getAuthority()!=Authority.最高权限){
			return;
		}
		try {
			accountorBL=BLServiceManager.getAccountorBLService(null);
		} catch (RemoteException | MalformedURLException | NotBoundException
				| SQLException e) {
			JOptionPane.showMessageDialog(this,e.getMessage());
			e.printStackTrace();
			return;
		}
		this.user=user;
		this.setLayout(null);
		this.setOpaque(false);
		
		textField = new JTextField();
		textField.setBounds(312, 18, 162, 21);
		add(textField);
		textField.setColumns(10);
		
		JButton button = new MyButton("Graphics/Button66-23.png");
		button.setText("查找");
		button.setBounds(493, 17, 66, 23);
		add(button);
		
		JButton button_1 = new MyButton("Graphics/Button66-23.png");
		button_1.setText("Graphics/Button93-23.png");
		button_1.setText("\u589E\u52A0\u8D26\u6237");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new AddAccountDialog(AccountorManagementPanel.this, accountorBL, user).setVisible(true);
			}
		});
		button_1.setBounds(84, 382, 93, 23);
		add(button_1);
		
		JButton button_2 = new MyButton("Graphics/Button66-23.png");
		button_2.setText("\u4FEE\u6539\u8D26\u6237");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int row=table.getSelectedRow();
				if(row==-1){
					return;
				}
				String name=(String)table.getValueAt(row, 0);
				new UpdateAccountDialog(AccountorManagementPanel.this, accountorBL, user,name).setVisible(true);
			}
		});
		button_2.setBounds(275, 382, 93, 23);
		add(button_2);
		
		JButton button_3 = new MyButton("Graphics/Button66-23.png");
		button_3.setText("\u5220\u9664\u8D26\u6237");
		button_3.setBounds(457, 382, 93, 23);
		button_3.addActionListener(e->deleteAccountor());
		add(button_3);
		
		table = new JTable();
		table.setBounds(64, 69, 505, 285);
		jsp=new JScrollPane(table);
		jsp.setBounds(64, 59, 505, 295);
		this.add(jsp);
		showAll();
	}
	

	private void deleteAccountor(){
		try {
			int row=table.getSelectedRow();
			if(row==-1){
				return;
			}
			AccountorVo accountor=new AccountorVo();
			accountor.setOperator(user.getName());
			accountor.set名称((String)table.getValueAt(row, 0));
			accountor.setOperator(user.getName());
			accountor.setOperation("删除账户");
			Vo result = accountorBL.deleteAccountor(accountor);
				if(result.isSuccessed()){
					JOptionPane.showMessageDialog(AccountorManagementPanel.this, "删除成功");
					this.showAll();
				}else{
					JOptionPane.showMessageDialog(AccountorManagementPanel.this, "删除失败\n"+result.getFailMessage());
				}
			
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(AccountorManagementPanel.this, "无法连接到服务器\n"+e.getMessage());
			e.printStackTrace();
		}
	}
	
	void showAll(){
		try {
			accounts=accountorBL.getAll();
			
			table.setModel(new DefaultTableModel(
				readAccountVo(accounts),
				new String[] {
					"名称", "金额"
				}
			));
		} catch (RemoteException e) {
			JOptionPane.showMessageDialog(this,
					"无法连接到服务器\n" + e.getMessage());
			e.printStackTrace();
		}
		
	}
	
	private String[][] readAccountVo(List<AccountorVo> account){
		String[][] result=new String[account.size()][2];
		for(int i=0;i<result.length;i++){
			result[i][0]=account.get(i).get名称();
			result[i][1]=tool.LongStringTransformer.LongToString(account.get(i).get金额());
		}
		return result;
	}
	
}
